package com.hilaver.dzmis.service.impl;

import java.util.List;

import com.hilaver.dzmis.Constants;
import com.hilaver.dzmis.basicinfo.BiAccessoryOthers;
import com.hilaver.dzmis.basicinfo.BiAccessoryZip;
import com.hilaver.dzmis.codetable.CtMaille;
import com.hilaver.dzmis.codetable.CtPuller;
import com.hilaver.dzmis.codetable.CtSlider;
import com.hilaver.dzmis.codetable.CtSupplyType;
import com.hilaver.dzmis.codetable.CtWeaveType;
import com.hilaver.dzmis.codetable.CtZipSize;
import com.hilaver.dzmis.codetable.CtZipType;
import com.hilaver.dzmis.dao.BiAccessoryZipDAO;
import com.hilaver.dzmis.service.AbstractBaseService;
import com.hilaver.dzmis.util.SimpleObj2XML;
import com.hilaver.dzmis.util.StringUtils;

public class BiAccessoryZipServiceImpl extends AbstractBaseService {

	public final static String COLUMN_MATERIAL_TYPE = "ctMaterialType";
	
	public final static String COLUMN_COLLAR_TYPE = "ctCollarType";

	public final static String COLUMN_BRAND = "ctBrand";
	
	public final static String COLUMN_MEASURE = "measure";
	
	public final static String COLUMN_LENGTH = "zipLength";

	public final static String COLUMN_IS_SPECIAL = "isSpecial";

	public final static String COLUMN_IS_PURCHASE = "isPurchase";

	public final static String COLUMN_REFERENCE = "reference";
	
	public final static String COLUMN_BLOCK_PHOTO_NAME= "blockPhotoName";
	
	public final static String COLUMN_HEAD_PHOTO_NAME = "headPhotoName";

	private CtServiceImpl ctService;
	
	private BiAccessoryZipDAO dao;

	public BiAccessoryZipServiceImpl() {
		this.ctService = new CtServiceImpl();
		this.dao = new BiAccessoryZipDAO();
	}

	@Override
	public String delete(int id) throws Exception {
		super.editSysMbox(BiAccessoryZip.class.getName(), id);
		return super.delete(BiAccessoryZip.class.getName(), id);
	}

	public String edit(BiAccessoryZip biAZ) throws Exception {
		String operation = null;
		if (biAZ.getId() != null) {
			BiAccessoryZip old = (BiAccessoryZip) this.dao.get(
					BiAccessoryZip.class.getName(), biAZ.getId());
			if(biAZ.getPhotoName() == null){
				biAZ.setPhoto(old.getPhoto());
				biAZ.setPhotoName(old.getPhotoName());
			}
			
			this.dao.getSession().evict(old);
			operation = "02";
		}else{
			operation = "01";
		}
		this.dao.saveOrUpdate(biAZ);
		super.editSysMbox(biAZ, operation);
		return "{success: true}";
	}

	@Override
	public String get(int id) throws Exception {
		BiAccessoryZip biAZ = (BiAccessoryZip) this.dao.get(
				BiAccessoryZip.class.getName(), id);

		writeFile(realFileDir + biAZ.getPhotoName(), biAZ.getPhoto());

		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		xmlSB.append(Constants.XML_ITEM_TAG[0]);

		if(biAZ.getBiFranceCustomer() != null){
			xmlSB.append("<" + COLUMN_CUSTOMER_ID + ">");
			xmlSB.append(biAZ.getBiFranceCustomer().getId());
			xmlSB.append("</" + COLUMN_CUSTOMER_ID + ">");
			
			xmlSB.append("<customerName>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(biAZ.getBiFranceCustomer().getName());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</customerName>");
			
			xmlSB.append("<customerDepartment>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(biAZ.getBiFranceCustomer().getDepartment()==null?"":biAZ.getBiFranceCustomer().getDepartment());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</customerDepartment>");
		}
		
		if(biAZ.getBiFactory() != null){
			xmlSB.append("<" + COLUMN_FACTORY_ID + ">");
			xmlSB.append(biAZ.getBiFactory().getId());
			xmlSB.append("</" + COLUMN_FACTORY_ID + ">");
			
			xmlSB.append("<factoryNameCN>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(biAZ.getBiFactory().getNameCN());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</factoryNameCN>");
			
			xmlSB.append("<factoryNameEN>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(biAZ.getBiFactory().getNameEN()==null?"":biAZ.getBiFactory().getNameEN());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</factoryNameEN>");
		}
		
		xmlSB.append("<" + COLUMN_PHOTO_NAME + ">");
		if (biAZ.getPhotoName() == null || "".equals(biAZ.getPhotoName())) {
			xmlSB.append(NO_IMAGE_NAME);
		} else {
			writeFile(realFileDir + biAZ.getPhotoName(), biAZ.getPhoto());
			xmlSB.append(biAZ.getPhotoName());
		}
		xmlSB.append("</" + COLUMN_PHOTO_NAME + ">");
		xmlSB.append(SimpleObj2XML.toXMLFragment(biAZ));
		xmlSB.append(Constants.XML_ITEM_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}

	public String getWithDescription(int id) throws Exception{
		BiAccessoryZip biAZ = (BiAccessoryZip) this.dao.get(
				BiAccessoryZip.class.getName(), id);

		writeFile(realFileDir + biAZ.getPhotoName(), biAZ.getPhoto());

		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		xmlSB.append(Constants.XML_ITEM_TAG[0]);
		
		xmlSB.append(this.toXMLFragment(biAZ));
		xmlSB.append(SimpleObj2XML.toXMLFragment(biAZ));
		
		xmlSB.append(Constants.XML_ITEM_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	
	@Override
	public String getAllPagination(int page, String sort, String order)
			throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	public String getAllPagination(int page, String sort, String order,
			String[] filters) throws Exception {
		int offset = Constants.PAGE_SIZE * (page - 1);
		List objList = this.dao.getAllPagination(BiAccessoryZip.class
				.getName(), offset, Constants.PAGE_SIZE, sort, order, filters);
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		for (Object obj : objList) {
			xmlSB.append(Constants.XML_ITEM_TAG[0]);
			xmlSB.append(Constants.XML_INDEX_TAG[0]);
			xmlSB.append(++offset);
			xmlSB.append(Constants.XML_INDEX_TAG[1]);

			xmlSB.append(this.toXMLFragment((BiAccessoryZip) obj));

			xmlSB.append(Constants.XML_ITEM_TAG[1]);
		}

		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[0]);
		xmlSB.append(page);
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[1]);

		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[0]);
		xmlSB.append(this.dao.getCount(BiAccessoryZip.class.getName(),
				filters));
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}

	public String toXMLFragment(BiAccessoryZip biAZ) throws Exception {
		StringBuffer xmlSB = new StringBuffer();
				
		if(biAZ.getBiFranceCustomer() != null){
			xmlSB.append("<" + COLUMN_CUSTOMER_NAME + ">");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(biAZ.getBiFranceCustomer().getName());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</" + COLUMN_CUSTOMER_NAME + ">");
			xmlSB.append("<" + COLUMN_CUSTOEMR_DEPARTMENT + ">");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(biAZ.getBiFranceCustomer().getDepartment()==null?"":biAZ.getBiFranceCustomer().getDepartment());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</" + COLUMN_CUSTOEMR_DEPARTMENT + ">");
		}
		
		if(biAZ.getBiFactory() != null){
			xmlSB.append("<factoryNameCN>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(biAZ.getBiFactory().getNameCN());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</factoryNameCN>");
			
			xmlSB.append("<factoryNameEN>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(biAZ.getBiFactory().getNameEN()==null?"":biAZ.getBiFactory().getNameEN());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</factoryNameEN>");
			
			xmlSB.append("<addressFactoryCN>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(biAZ.getBiFactory().getAddressFactoryCN());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</addressFactoryCN>");
			
			xmlSB.append("<addressFactoryEN>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(biAZ.getBiFactory().getAddressFactoryEN());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</addressFactoryEN>");
		}
		
		xmlSB.append("<ctMaille>");
		xmlSB.append(this.ctService.getDescription(CtMaille.class
				.getName(), biAZ.getCtMaille(), this.locale));
		xmlSB.append("</ctMaille>");
		
		xmlSB.append("<ctZipType>");
		xmlSB.append(this.ctService.getDescription(CtZipType.class
				.getName(), biAZ.getCtZipType(), this.locale));
		xmlSB.append("</ctZipType>");
		
		xmlSB.append("<ctZipSize>");
		xmlSB.append(this.ctService.getDescription(CtZipSize.class
				.getName(), biAZ.getCtZipSize(), this.locale));
		xmlSB.append("</ctZipSize>");
		
		xmlSB.append("<ctPuller>");
		xmlSB.append(this.ctService.getDescription(CtPuller.class
				.getName(), biAZ.getCtPuller(), this.locale));
		xmlSB.append("</ctPuller>");
		
		xmlSB.append("<ctSlider>");
		xmlSB.append(this.ctService.getDescription(CtSlider.class
				.getName(), biAZ.getCtSlider(), this.locale));
		xmlSB.append("</ctSlider>");
		
		xmlSB.append("<ctWeaveType>");
		xmlSB.append(this.ctService.getDescription(CtWeaveType.class
				.getName(), biAZ.getCtWeaveType(), this.locale));
		xmlSB.append("</ctWeaveType>");

		xmlSB.append("<ctSupplyType>");
		xmlSB.append(this.ctService.getDescription(CtSupplyType.class.getName(),
				biAZ.getCtSupplyType(), this.locale));
		xmlSB.append("</ctSupplyType>");

		xmlSB.append("<ctSupplyTypeCode>");
		xmlSB.append(biAZ.getCtSupplyType());
		xmlSB.append("</ctSupplyTypeCode>");
		
		xmlSB.append("<" + COLUMN_PHOTO_NAME + ">");
		if (biAZ.getPhotoName() == null || "".equals(biAZ.getPhotoName())) {
			xmlSB.append(NO_IMAGE_NAME);
		} else {
			writeFile(realFileDir + biAZ.getPhotoName(), biAZ.getPhoto());
			xmlSB.append(biAZ.getPhotoName());
		}
		xmlSB.append("</" + COLUMN_PHOTO_NAME + ">");
				
		xmlSB.append(SimpleObj2XML.toXMLFragment(biAZ));
		
		return xmlSB.toString();
	}
	
	public String getMaxReferenceIndex(String ctZipType) throws Exception{
		List<Integer> referenceIndexList = this.dao.getMaxReferenceIndex(ctZipType);
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		xmlSB.append(Constants.XML_ITEM_TAG[0]);
		xmlSB.append(Constants.XML_CDATA[0]);
		if(referenceIndexList.size() == 0 || referenceIndexList.get(0) == null){
			xmlSB.append("0001");
		}else{
			Integer index = referenceIndexList.get(0);
			xmlSB.append(StringUtils.to4Digital(++index));
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append(Constants.XML_ITEM_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	
	public static void main(String[] args) throws Exception{
		BiAccessoryZipServiceImpl bzs = new BiAccessoryZipServiceImpl();
		System.out.println(bzs.get(18));
	}

}
